package com.foreverwin.mes.so.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.meapi.model.Sfc;
import com.foreverwin.mes.so.model.ShopOrderDispatchDetail;
import com.foreverwin.mes.so.service.ShopOrderDispatchDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Syngna
 * @since 2020-02-06
 */
@Controller
@RequestMapping("/shopOrderDispatchDetails")
public class ShopOrderDispatchDetailController {
    private final Logger logger = LoggerFactory.getLogger(ShopOrderDispatchDetailController.class);

    @Autowired
    public ShopOrderDispatchDetailService shopOrderDispatchDetailService;

    /**
     * 根据id查询
     *
     * @param id 主键
     * @return
     */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getShopOrderDispatchDetailById(@PathVariable String id) {
        ShopOrderDispatchDetail result;
        try {
            result = shopOrderDispatchDetailService.selectById(id);
        } catch (Exception e) {
            logger.error("getShopOrderDispatchDetailById -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getShopOrderDispatchDetailList(ShopOrderDispatchDetail shopOrderDispatchDetail) {
        List<ShopOrderDispatchDetail> result;
        try {
            EntityWrapper<ShopOrderDispatchDetail> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(shopOrderDispatchDetail);
            result = shopOrderDispatchDetailService.selectList(EntityWrapper);
        } catch (Exception e) {
            logger.error("getShopOrderDispatchDetailList -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage 分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getShopOrderDispatchDetailPageList")
    public AjaxResult getShopOrderDispatchDetailPageList(FrontPage<ShopOrderDispatchDetail> frontPage, ShopOrderDispatchDetail shopOrderDispatchDetail) {
        Page result;
        try {
            String site = CommonMethods.getSite();
            shopOrderDispatchDetail.setSite(site);
            EntityWrapper<ShopOrderDispatchDetail> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(shopOrderDispatchDetail);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                //TODO modify global query
                EntityWrapper
                        .like("handle", frontPage.getGlobalQuery())
                        .or().like("site", frontPage.getGlobalQuery())
                        .or().like("shopOrder", frontPage.getGlobalQuery())
                        .or().like("resrce", frontPage.getGlobalQuery())
                        .or().like("itemBo", frontPage.getGlobalQuery())
                        .or().like("dispTaskNo", frontPage.getGlobalQuery())
                        .or().like("dispTaskChildNo", frontPage.getGlobalQuery())
                        .or().like("scheduleQty", frontPage.getGlobalQuery())
                        .or().like("status", frontPage.getGlobalQuery())
                        .or().like("recipeStatus", frontPage.getGlobalQuery())
                        .or().like("creator", frontPage.getGlobalQuery())
                        .or().like("updater", frontPage.getGlobalQuery())
                ;
            }
            result = shopOrderDispatchDetailService.selectPage(frontPage.getPagePlus(), EntityWrapper);
        } catch (Exception e) {
            logger.error("getShopOrderDispatchDetailPageList -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 保存和修改公用
     *
     * @param shopOrderDispatchDetail 传递的实体
     * @return null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult shopOrderDispatchDetailSave(@RequestBody ShopOrderDispatchDetail shopOrderDispatchDetail) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        int count;
        try {
            count = shopOrderDispatchDetailService.insertOrUpdate(shopOrderDispatchDetail) ? 1 : 0;
        } catch (Exception e) {
            logger.error("shopOrderDispatchDetailSave -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(count == 0 ? null : shopOrderDispatchDetail);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public AjaxResult shopOrderDispatchDetailDelete(@PathVariable("id") String id) {
        int count;
        try {
            count = shopOrderDispatchDetailService.deleteById(id) ? 1 : 0;
        } catch (Exception e) {
            logger.error("shopOrderDispatchDetailDelete -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("count: " + count);
    }

    /**
     * 批量删除对象
     *
     * @param ids 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchDelete")
    public AjaxResult removeByIds(List<String> ids) {
        int count;
        try {
            count = shopOrderDispatchDetailService.deleteBatchIds(ids) ? 1 : 0;
        } catch (Exception e) {
            logger.error("shopOrderDispatchDetailBatchDelete -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("count: " + count);
    }

    @GetMapping("/autoStartSfc")
    @ResponseBody
    public AjaxResult autoStartSfc(String operation, String resource, String so, String lot) {
        try {
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            shopOrderDispatchDetailService.sfcAutoStart(site, user, operation, resource, so, lot);
        } catch (Exception e) {
            logger.error("autoStartSfc -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 手动开始SFC
     * @param operation
     * @param resource
     * @param sfc 任务号或者生产批
     * @return
     */
    @GetMapping("/manualStartSfc")
    @ResponseBody
    public AjaxResult manualStartSfc(String operation, String resource, String sfc) {
        List<String> sfcNoList = new ArrayList<>();
        try {
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            sfcNoList = shopOrderDispatchDetailService.sfcManualStart(site, user, operation, resource, sfc);
        } catch (Exception e) {
            logger.error("manualStartSfc -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(sfcNoList);
    }
}